package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/user")
@RestController
public class UserController {

    @Reference
    private MenuService menuService;

    @RequestMapping("/getUsername")
    public Result getUsername(){
        // getAuthentication 认证信息对象
        //getPrincipal user对象
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = user.getUsername();
        if(user!=null){
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
        }
        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }

    @RequestMapping("/getMenu")
    public Result getMenu(){
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = user.getUsername();
        // 通过用户名查询用户菜单
        List<Map> list = menuService.findByUsername(username);
        if(user!=null){
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,list);
        }
        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }

}
